<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
</head>

<body>
    <div id="test">

    </div>
    <script type="text/babel">

        class Weather extends React.Component {

            /* 
                调整1-原方案
                constructor(props){
                    super(props);
                    this.state={ isHot:true}
                    this.changeWeather = this.changeWeather.bind(this)
                }   
            */ 
            // 调整1-优化
            state = { isHot: true }

            render() {
                return <h1 onClick={this.changeWeather}>今天天气很{this.state.isHot ? '炎热' : '凉爽'}</h1>
            }
            /* 
            // 调整2-原方案
                changeWeather(){
                    console.log('@',this);
                    const isHot = this.state.isHot
                    this.setState({isHot:!isHot})
                } 
            */
            // 调整2-优化
            // 组件类中定义的时间回调，必须写成赋值语句+箭头函数，避免了this为undefined的问题
            changeWeather =  ()=> {
                // console.log(this);// 类的实例对象
                const isHot = this.state.isHot
                this.setState({ isHot: !isHot })
            }
        }

        ReactDOM.render(<Weather />, document.getElementById('test'))

    </script>
</body>

</html>